package com.eascs.workflow.processAuthorization.controller;


import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.common.util.StringUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.workflow.common.entity.wf.DicData;
import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.dictionary.service.DicDataServiceInlet;
import com.eascs.workflow.processAuthorization.service.IProcessAuthorizationService;
import com.eascs.workflow.processAuthorization.vo.ProcessAuthorizationVo;
import com.eascs.workflow.sys.service.SysPermissionServiceInlet;
import com.eascs.workflow.wf.service.TemplateService;

@RestController
@RequestMapping("/rProcessAuth")
public class ProcessAuthorizationRestController extends BaseRestController {
	
	@Inject
	private TemplateService templateService;
	
	@Inject
	private SysPermissionServiceInlet sysPermissionServiceInlet;
	
	@Inject
	private IProcessAuthorizationService processAuthorizationService;
	
	@Inject
	private DicDataServiceInlet dicDataService;
	
	
	
	 @RequestMapping("/addAuth")	
	 public HtmlRestResult addAuth(ProcessAuthorizationVo vo){
		   List<DicData> catagoryList = dicDataService.getDicDataByType("processCatagory").getModel();
		   ViewContext.put("catagoryList", catagoryList);
		   return  HtmlRestResult.popHtmlFragment("processAuth/add");
	}
	   
	
	@RequestMapping(value = "/getProccessType", method = RequestMethod.GET)
	public BaseRestResult<List<DicData>> getProcessTypeByCatagory(@RequestParam("catagory") String catagory) {
		List<DicData> data = new ArrayList<DicData>();
		if (StringUtil.hasText(catagory)) {
			ModelResult<List<DicData>> modelResult = templateService.getProcessTypeByCatagory(catagory);
			data = modelResult.getModel();
		}
		return BaseRestResult.buildRestResult(data);
	}
	
	@RequestMapping(value = "/outor")
	public BaseRestResult<List<Object[]>> outor(ProcessAuthorizationVo vo) {
		List<Object[]> data = new ArrayList<>();
		if (StringUtil.hasText(vo.getRollOuterName())) {
			ModelResult<List<Object[]>> da = sysPermissionServiceInlet.getOperatorList(vo.getRollOuterName());
			data = da.getModel();
		}
		return BaseRestResult.buildRestResult(data);
	}
	
	@RequestMapping(value = "/inor")
	public BaseRestResult<List<Object[]>> inor(ProcessAuthorizationVo vo) {
		List<Object[]> data = new ArrayList<>();
		if (StringUtil.hasText(vo.getRollInerName())) {
			ModelResult<List<Object[]>> da = sysPermissionServiceInlet.getOperatorList(vo.getRollInerName());
			data = da.getModel();
		}
		return BaseRestResult.buildRestResult(data);
	}
	
	@RequestMapping(value = "/save")
	public BaseRestResult<String> saveOrupdateCustomerBasicInfo(ProcessAuthorizationVo vo) {
		if (StringUtil.isBlank(vo.getProcessMainCode())) {
			return BaseRestResult.warn("流程大类不能为空");
		}
		if (StringUtil.isBlank(vo.getProcessLessCode())) {
			return BaseRestResult.warn("流程类型不能为空");
		}
		if (StringUtil.isBlank(vo.getRollInerCode())) {
			return BaseRestResult.warn("转出人不能为空");
		}
		if (StringUtil.isBlank(vo.getRollOuterCode())) {
			return BaseRestResult.warn("转入人不能为空");
		}
		if (vo.getAuthBeginDate()==null) {
			return BaseRestResult.warn("授权开始时间不能为空");
		}
		if (vo.getAuthEndDate()==null) {
			return BaseRestResult.warn("授权结束时间不能为空");
		}
		if (StringUtil.isBlank(vo.getAuthStatus())) {
			return BaseRestResult.warn("生效状态不能为空");
		}
		
		ModelResult<Boolean> model = processAuthorizationService.saveProcessAuth(vo);
		if(model.isSuccess()){
			return BaseRestResult.info("保存成功").andRefresh();
		}else{
			if("exist".equals(model.getErrCode())){
				return BaseRestResult.warn("您已经在这个时间段将此类型流程授权了，不能重复授权！");
			}else{
				return BaseRestResult.error("数据库异常");
			}
		}
	}
   
	
   @RequestMapping("/editAuth")	
   public BaseRestResult<String> editAuth(ProcessAuthorizationVo vo){
	 
	  ModelResult<Boolean> model =  processAuthorizationService.updateProcessAuthStatus(vo); 	   
		if(model.isSuccess()){
			return BaseRestResult.info("修改成功").andRefresh();
		}else{
			if("exist".equals(model.getErrCode())){
				return BaseRestResult.warn("您已经在这个时间段将此类型流程授权了，不能重复授权！");
			}else{
				return BaseRestResult.error("数据库异常");
			}
		}   
   }
	
	
	
}